package com.itheima.demo5;

import java.io.*;
import java.util.ArrayList;

public class IODemo14 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //序列化流
        //创建对象
        Student stu1 = new Student("张三", 23,"南京");
        Student stu2 = new Student("李四", 24,"北京");
        Student stu3 = new Student("王五", 23,"深圳");

        //创建对象操作输出流/序列化流的对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"
        ));
        ArrayList<Student> alist = new ArrayList<>();
        alist.add(stu1);
        alist.add(stu2);
        alist.add(stu3);

        oos.writeObject(alist);

        oos.close();


        //反序列化流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));

        ArrayList<Student> s = (ArrayList<Student>) ois.readObject();


        for (Student student : s) {
            System.out.println(student);
        }

        ois.close();
    }
}
